package com.pine.app.module.security.oauth.provider.authrizationCode;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author xiaoyuan
 * @create 2019/10/18 14:30
 **/

public class InMemoryAuthenticationCodeStore implements AuthenticationCodeStore {

    private final Cache<String, AuthenticationCode> cache;
    private Long expireIn = 10L * 60L;

    public InMemoryAuthenticationCodeStore() {
        cache = CacheBuilder.newBuilder().expireAfterWrite(expireIn, TimeUnit.SECONDS).build();
    }

    @Override
    public AuthenticationCode getByCode(String code) {
        return cache.getIfPresent(code);
    }

    @Override
    public void storeCode(AuthenticationCode oauthCode) {
        cache.put(oauthCode.getCode(), oauthCode);
    }

    @Override
    public void removeByCode(String code) {
        cache.invalidate(code);
    }
}
